package view.supply;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import main.WinkelApplication;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import model.Product;
import view.Manage;

/**
 *
 * @author Kevin
 */
public class SupplyPanel extends JPanel {
    
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    JTable table;
    int productId;
    
    public SupplyPanel() {
        super();
        toSetLayout();
    }
    
    public void toSetLayout() {
        
        setLayout(new BorderLayout());
        add(getTitle(), BorderLayout.PAGE_START);
        add(initTable(), BorderLayout.CENTER);
        add(pnlAddButton(), BorderLayout.PAGE_END);
    }
    
    
    private JLabel getTitle() {
        JLabel title = new JLabel("Overzicht voorraad", SwingConstants.CENTER);
        title.setFont(WinkelApplication.computeFontSize(2));

        return title;
    }
    
    private JScrollPane initTable() {        
        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new GridLayout());
        String statusText;
        
        String[] columNames = {"id" , "Categorie-id", "Product", "Prijs", "omschrijving", "Voorraad", "status"};
        java.util.List<Product> allProduct = WinkelApplication.getQueryManager().getAllProducts();

        Object[][] data = new Object[allProduct.size()][];
        Object[] prr = new Object[allProduct.size()];
        
        for( int i = 0; i < allProduct.size(); i++) {
            Product product = allProduct.get(i);
            
            if(product.getStatus() == 1)
                statusText = "Beschikbaar";
            else
                statusText = "Niet beschikbaar";
            
            
               // prr[i] = new Object[] { product.getName() };
                data[i] = new Object[]{ product.getProductId(), 
                                        WinkelApplication.getQueryManager()
                                            .getCategoryName(product.getCategorieId()), 
                                        product.getName(), 
                                        product.getPrice(),
                                        product.getDescription(), 
                                        product.getVoorraad(), 
                                        statusText 
                };
        }
        
        table = new JTable( data, columNames) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        
        table.setFillsViewportHeight(true);
        
        table.setRowHeight(25);
        table.setFont(WinkelApplication.FONT_14_PLAIN);
        table.setFillsViewportHeight(true);
        
       JScrollPane scrollPane = new JScrollPane(table);
      
        return scrollPane;
    }
    
    private JPanel pnlAddButton() {
        JPanel panel= new JPanel();
        
        JButton toevoegen = new JButton("Toevoegen");
        toevoegen.setPreferredSize(new Dimension(150, 50));
        toevoegen.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                voegProduct();
            }

            private void voegProduct() {
                     JFrame frame = new JFrame();
                     frame.add(new view.supply.SupplyAdd());
                     frame.setResizable(false);
                     frame.setLocation((dim.width/2)-230, (dim.height/2)-230);
                     
                     frame.setSize(430, 270);
                     frame.setVisible(true);
            }
            
        });        
        
        JButton wijzigen = new JButton("Wijzigen");
        wijzigen.setPreferredSize(new Dimension(150, 50));
        wijzigen.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                int row;
                if((row = table.getSelectedRow()) == -1)
                    return;
                
                int prod_id = new Integer(table.getValueAt(row, 0).toString());
                
                productEdit(prod_id);
            }

            private void productEdit(int prod_id) {
                JFrame frame = new JFrame();
                frame.setSize(430, 270);
                frame.setLocation((dim.width/2)-230, (dim.height/2)-230);
                frame.setResizable(false);
                frame.setVisible(true);

                frame.getContentPane().add(new SupplyEdit(prod_id));
                frame.validate(); 
                frame.repaint();
            }

        });
        
        JButton verwijderen = new JButton("Verwijderen");
        verwijderen.setPreferredSize(new Dimension(150, 50));
        verwijderen.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                int row;
                if((row = table.getSelectedRow()) == -1) 
                    return;
                
                int prod_id = new Integer(table.getValueAt(row, 0).toString());
                deleteOrder(prod_id);
            }
            
            private void deleteOrder(int prod_id) {
                int response = JOptionPane.showConfirmDialog(
                        null,
                        "Weet je zeker dat je deze product wilt verwijderen?",
                        "Verwijder product",
                        JOptionPane.YES_NO_OPTION
                );          
                if (response == JOptionPane.YES_OPTION )
                    {
                    WinkelApplication.getQueryManager().removeProduct(prod_id);
                    WinkelApplication.getInstance().showPanel(new view.supply.SupplyPanel());
                    JOptionPane.showMessageDialog(null,
                    "De product is verwijderd!","Verwijderd",
                    JOptionPane.INFORMATION_MESSAGE);
                    } 
        }
            
        });
        
        JButton terug = new JButton("");
        terug.setPreferredSize(new Dimension(150, 50));
        if(WinkelApplication.getInstance().getAccesLevel()==2){
            terug.setText("Log Uit");
        } else {
            terug.setText("Terug naar Beheer");
        }
        terug.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e)
            {
                if (WinkelApplication.getInstance().getAccesLevel()==2){
                    WinkelApplication instance = WinkelApplication.getInstance();
                    instance.showPanel(new view.LogInScreen());
                } else {
                    WinkelApplication instance = WinkelApplication.getInstance();
                    instance.showPanel(new view.Manage(WinkelApplication.getInstance().getAccesLevel()));
                }
                
            }
        });
        
        panel.add(toevoegen);
        panel.add(wijzigen);
        panel.add(verwijderen);
        panel.add(terug);
        
        return panel;
    }
    
}